import java.util.*;

public class BinaryTree {
    static class TreeNode {
//        public char val;
        public int val;
        public TreeNode left;
        public TreeNode right;
//        public TreeNode(char val) {
        public TreeNode(int val) {
            this.val = val;
        }
    }
    /**
     * 构建二叉树(穷举)
     */
    public TreeNode createBinaryTree() {
//        TreeNode A = new TreeNode('A');
//        TreeNode B = new TreeNode('B');
//        TreeNode C = new TreeNode('C');
//        TreeNode D = new TreeNode('D');
//        TreeNode E = new TreeNode('E');
//        TreeNode F = new TreeNode('F');
//        TreeNode G = new TreeNode('G');
//        TreeNode H = new TreeNode('H');
//
//        A.left = B;
//        A.right = C;
//
//        B.left = D;
//        B.right = E;
//
//        C.left = F;
//        C.right = G;
//
//        E.right = H;
//
//        return A;
        TreeNode A = new TreeNode(1);
        TreeNode B = new TreeNode(2);
        TreeNode C = new TreeNode(2);
        TreeNode D = new TreeNode(3);
        TreeNode E = new TreeNode(3);

        A.left = B;
        A.right = C;

        B.right = D;

        C.right = E;
        return A;
    }

    /**
     * 层序遍历
     *      通过队列实现
     *      1.入队列头节点
     *      2.出队列,打印,并代入左右不为null的左右子树
     *      3.队列为空,结束遍历
     */
    public void levelOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");

            if(cur.left != null) {
                queue.offer(cur.left);
            }
            if(cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }
    /**
     1.根入队
     2.求队列此时个数,出队次数根据个数决定
     3.使用一维数组接收
     4.使用二维数组接收
     */
//    public List<List<Character>> levelOrder2(TreeNode root) {
//        List<List<Character>> retList = new ArrayList<>();
//        if(root == null) {
//            return retList;
//        }
//        Queue<TreeNode> queue = new LinkedList<>();
//        queue.offer(root);
//
//        while(!queue.isEmpty()) {
//            int size = queue.size();
//            List<Character> list = new ArrayList<>();
//
//            while(size != 0) {
//                TreeNode cur = queue.poll();
//                list.add(cur.val);
//                if(cur.left != null) {
//                    queue.offer(cur.left);
//                }
//                if(cur.right != null) {
//                    queue.offer(cur.right);
//                }
//                size--;
//            }
//            retList.add(list);
//        }
//        return retList;
//    }
    /**
     * 平衡二叉树()初阶
     O(N ^ 2)
     1.求左子树高度
     2.求右子树高度
     3.判断是否超过1,且左右子树的子树高度差均不超过1
     */
    public boolean isBalanced(TreeNode root) {
        if(root == null) {
            return true;
        }
        int leftHeight = getTreeHeight(root.left);
        int rightHeight = getTreeHeight(root.right);

        return Math.abs(leftHeight - rightHeight) <= 1
                && isBalanced(root.left)
                && isBalanced(root.right);
    }
    /**
     求树高度
     1.判断是否为空
     2.递归左右子树
     */
    private int getTreeHeight(TreeNode root) {
        if(root == null) {
            return 0;
        }

        int leftHeight = getTreeHeight(root.left);
        int rightHeight = getTreeHeight(root.right);
        return Math.max(leftHeight,rightHeight) + 1;
    }
    /**
     平衡二叉树进阶
     O(N)
     1.求左子树高度
     2.求右子树高度
     3.判断是否超过1,且左右子树的子树高度差均不超过1
     */
    public boolean isBalanced2(TreeNode root) {
        if(root == null) {
            return true;
        }
        int leftHeight = getTreeHeight(root.left);
        int rightHeight = getTreeHeight(root.right);
        return leftHeight >=0
                && rightHeight >= 0
                && Math.abs(leftHeight - rightHeight) <= 1;
    }
    /**
     求树高度
     1.判断是否为空
     2.递归左右子树
     3.判断是否高度超过1,超过1返回-1.
     4.没超过正常返回
     */
    private int getTreeHeight2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int leftHeight = getTreeHeight(root.left);
        int rightHeight = getTreeHeight(root.right);
        if(leftHeight >=0 && rightHeight >= 0
                && Math.abs(leftHeight - rightHeight) <= 1) {
            return Math.max(leftHeight,rightHeight) + 1;
        }else {
            return -1;
        }
    }
    /**
     对称二叉树
     1.判断头左右
     2.判断根左与对称根右 根右与对称根左
     */
    public boolean isSymmetric(TreeNode root) {
        if(root == null) {
            return true;
        }
        return isSymmetricChild(root.left,root.right);
    }
    /**
     1.判断是否为null
     2.一个为null,一个不为null情况
     3.判断值
     4.递归判断左右
     */
    private boolean isSymmetricChild(TreeNode rootLeft,TreeNode rootRight) {
        if(rootLeft == null && rootRight == null) {
            return true;
        }
        if(rootLeft == null && rootRight != null
                || rootLeft != null && rootRight == null) {
            return false;
        }
        if(rootLeft.val != rootRight.val) {
            return false;
        }
        return isSymmetricChild(rootLeft.left,rootRight.right)
                && isSymmetricChild(rootLeft.right,rootRight.left);
    }
}
